home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / binutils.7 / binutils / binutils-2.7 / bfd / stabs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-04  |  14.3 KB  |  529 lines

  1. /* Stabs in sections linking support.
  2.    Copyright 1996 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  20.  
  21. /* This file contains support for linking stabs in sections, as used
  22.    on COFF and ELF.  */
  23.  
  24. #include "bfd.h"
  25. #include "sysdep.h"
  26. #include "libbfd.h"
  27. #include "aout/stab_gnu.h"
  28.  
  29. #include <ctype.h>
  30.  
  31. /* Stabs entries use a 12 byte format:
  32.      4 byte string table index
  33.      1 byte stab type
  34.      1 byte stab other field
  35.      2 byte stab desc field
  36.      4 byte stab value
  37.    FIXME: This will have to change for a 64 bit object format.
  38.  
  39.    The stabs symbols are divided into compilation units.  For the
  40.    first entry in each unit, the type of 0, the value is the length of
  41.    the string table for this unit, and the desc field is the number of
  42.    stabs symbols for this unit.  */
  43.  
  44. #define STRDXOFF (0)
  45. #define TYPEOFF (4)
  46. #define OTHEROFF (5)
  47. #define DESCOFF (6)
  48. #define VALOFF (8)
  49. #define STABSIZE (12)
  50.  
  51. /* A hash table used for header files with N_BINCL entries.  */
  52.  
  53. struct stab_link_includes_table
  54. {
  55.   struct bfd_hash_table root;
  56. };
  57.  
  58. /* A linked list of totals that we have found for a particular header
  59.    file.  */
  60.  
  61. struct stab_link_includes_totals
  62. {
  63.   struct stab_link_includes_totals *next;
  64.   bfd_vma total;
  65. };
  66.  
  67. /* An entry in the header file hash table.  */
  68.  
  69. struct stab_link_includes_entry
  70. {
  71.   struct bfd_hash_entry root;
  72.   /* List of totals we have found for this file.  */
  73.   struct stab_link_includes_totals *totals;
  74. };
  75.  
  76. /* Look up an entry in an the header file hash table.  */
  77.  
  78. #define stab_link_includes_lookup(table, string, create, copy) \
  79.   ((struct stab_link_includes_entry *) \
  80.    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
  81.  
  82. /* This structure is used to hold a list of N_BINCL symbols, some of
  83.    which might be converted into N_EXCL symbols.  */
  84.  
  85. struct stab_excl_list
  86. {
  87.   /* The next symbol to convert.  */
  88.   struct stab_excl_list *next;
  89.   /* The offset to this symbol in the section contents.  */
  90.   bfd_size_type offset;
  91.   /* The value to use for the symbol.  */
  92.   bfd_vma val;
  93.   /* The type of this symbol (N_BINCL or N_EXCL).  */
  94.   int type;
  95. };
  96.  
  97. /* This structure is stored with each .stab section.  */
  98.  
  99. struct stab_section_info
  100. {
  101.   /* This is a linked list of N_BINCL symbols which should be
  102.      converted into N_EXCL symbols.  */
  103.   struct stab_excl_list *excls;
  104.   /* This is an array of string indices.  For each stab symbol, we
  105.      store the string index here.  If a stab symbol should not be
  106.      included in the final output, the string index is -1.  */
  107.   bfd_size_type stridxs[1];
  108. };
  109.  
  110. /* This structure is used to keep track of stabs in sections
  111.    information while linking.  */
  112.  
  113. struct stab_info
  114. {
  115.   /* A hash table used to hold stabs strings.  */
  116.   struct bfd_strtab_hash *strings;
  117.   /* The header file hash table.  */
  118.   struct stab_link_includes_table includes;
  119.   /* The first .stabstr section.  */
  120.   asection *stabstr;
  121. };
  122.  
  123. static struct bfd_hash_entry *stab_link_includes_newfunc
  124.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  125.  
  126. /* The function to create a new entry in the header file hash table.  */
  127.  
  128. static struct bfd_hash_entry *
  129. stab_link_includes_newfunc (entry, table, string)
  130.      struct bfd_hash_entry *entry;
  131.      struct bfd_hash_table *table;
  132.      const char *string;
  133. {
  134.   struct stab_link_includes_entry *ret =
  135.     (struct stab_link_includes_entry *) entry;
  136.  
  137.   /* Allocate the structure if it has not already been allocated by a
  138.      subclass.  */
  139.   if (ret == (struct stab_link_includes_entry *) NULL)
  140.     ret = ((struct stab_link_includes_entry *)
  141.        bfd_hash_allocate (table,
  142.                   sizeof (struct stab_link_includes_entry)));
  143.   if (ret == (struct stab_link_includes_entry *) NULL)
  144.     return (struct bfd_hash_entry *) ret;
  145.  
  146.   /* Call the allocation method of the superclass.  */
  147.   ret = ((struct stab_link_includes_entry *)
  148.      bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  149.   if (ret)
  150.     {
  151.       /* Set local fields.  */
  152.       ret->totals = NULL;
  153.     }
  154.  
  155.   return (struct bfd_hash_entry *) ret;
  156. }
  157.  
  158. /* This function is called for each input file from the add_symbols
  159.    pass of the linker.  */
  160.  
  161. boolean
  162. _bfd_link_section_stabs (abfd, psinfo, stabsec, stabstrsec, psecinfo)
  163.      bfd *abfd;
  164.      PTR *psinfo;
  165.      asection *stabsec;
  166.      asection *stabstrsec;
  167.      PTR *psecinfo;
  168. {
  169.   struct stab_info *sinfo;
  170.   bfd_size_type count;
  171.   struct stab_section_info *secinfo;
  172.   bfd_byte *stabbuf = NULL;
  173.   bfd_byte *stabstrbuf = NULL;
  174.   bfd_byte *sym, *symend;
  175.   bfd_size_type stroff, next_stroff, skip;
  176.   bfd_size_type *pstridx;
  177.  
  178.   if (stabsec->_raw_size == 0
  179.       || stabstrsec->_raw_size == 0)
  180.     {
  181.       /* This file does not contain stabs debugging information.  */
  182.       return true;
  183.     }
  184.  
  185.   if (stabsec->_raw_size % STABSIZE != 0)
  186.     {
  187.       /* Something is wrong with the format of these stab symbols.
  188.          Don't try to optimize them.  */
  189.       return true;
  190.     }
  191.  
  192.   if ((stabstrsec->flags & SEC_RELOC) != 0)
  193.     {
  194.       /* We shouldn't see relocations in the strings, and we aren't
  195.          prepared to handle them.  */
  196.       return true;
  197.     }
  198.  
  199.   if (*psinfo == NULL)
  200.     {
  201.       /* Initialize the stabs information we need to keep track of.  */
  202.       *psinfo = (PTR) bfd_alloc (abfd, sizeof (struct stab_info));
  203.       sinfo = (struct stab_info *) *psinfo;
  204.       sinfo->strings = _bfd_stringtab_init ();
  205.       if (sinfo->strings == NULL)
  206.     goto error_return;
  207.       if (! bfd_hash_table_init_n (&sinfo->includes.root,
  208.                    stab_link_includes_newfunc,
  209.                    251))
  210.     goto error_return;
  211.       sinfo->stabstr = bfd_make_section_anyway (abfd, ".stabstr");
  212.       sinfo->stabstr->flags |= SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING;
  213.     }
  214.  
  215.   sinfo = (struct stab_info *) *psinfo;
  216.  
  217.   /* Initialize the information we are going to store for this .stab
  218.      section.  */
  219.  
  220.   count = stabsec->_raw_size / STABSIZE;
  221.  
  222.   *psecinfo = bfd_alloc (abfd,
  223.              (sizeof (struct stab_section_info)
  224.               + (count - 1) * sizeof (bfd_size_type)));
  225.   if (*psecinfo == NULL)
  226.     goto error_return;
  227.  
  228.   secinfo = (struct stab_section_info *) *psecinfo;
  229.   secinfo->excls = NULL;
  230.   memset (secinfo->stridxs, 0, count * sizeof (bfd_size_type));
  231.  
  232.   /* Read the stabs information from abfd.  */
  233.  
  234.   stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size);
  235.   stabstrbuf = (bfd_byte *) bfd_malloc (stabstrsec->_raw_size);
  236.   if (stabbuf == NULL || stabstrbuf == NULL)
  237.     goto error_return;
  238.  
  239.   if (! bfd_get_section_contents (abfd, stabsec, stabbuf, 0,
  240.                   stabsec->_raw_size)
  241.       || ! bfd_get_section_contents (abfd, stabstrsec, stabstrbuf, 0,
  242.                      stabstrsec->_raw_size))
  243.     goto error_return;
  244.  
  245.   /* Look through the stabs symbols, work out the new string indices,
  246.      and identify N_BINCL symbols which can be eliminated.  */
  247.  
  248.   stroff = 0;
  249.   next_stroff = 0;
  250.   skip = 0;
  251.  
  252.   symend = stabbuf + stabsec->_raw_size;
  253.   for (sym = stabbuf, pstridx = secinfo->stridxs;
  254.        sym < symend;
  255.        sym += STABSIZE, ++pstridx)
  256.     {
  257.       int type;
  258.       const char *string;
  259.  
  260.       if (*pstridx != 0)
  261.     {
  262.       /* This symbol has already been handled by an N_BINCL pass.  */
  263.       continue;
  264.     }
  265.  
  266.       type = sym[TYPEOFF];
  267.  
  268.       if (type == 0)
  269.     {
  270.       /* Special type 0 stabs indicate the offset to the next
  271.              string table.  We don't copy these into the output file.  */
  272.       stroff = next_stroff;
  273.       next_stroff += bfd_get_32 (abfd, sym + 8);
  274.       *pstridx = (bfd_size_type) -1;
  275.       ++skip;
  276.       continue;
  277.     }
  278.  
  279.       /* Store the string in the hash table, and record the index.  */
  280.       string = ((char *) stabstrbuf
  281.         + stroff
  282.         + bfd_get_32 (abfd, sym + STRDXOFF));
  283.       *pstridx = _bfd_stringtab_add (sinfo->strings, string, true, true);
  284.  
  285.       /* An N_BINCL symbol indicates the start of the stabs entries
  286.      for a header file.  We need to scan ahead to the next N_EINCL
  287.      symbol, ignoring nesting, adding up all the characters in the
  288.      symbol names, not including the file numbers in types (the
  289.      first number after an open parenthesis).  */
  290.       if (type == N_BINCL)
  291.     {
  292.       bfd_vma val;
  293.       int nest;
  294.       bfd_byte *incl_sym;
  295.       struct stab_link_includes_entry *incl_entry;
  296.       struct stab_link_includes_totals *t;
  297.       struct stab_excl_list *ne;
  298.  
  299.       val = 0;
  300.       nest = 0;
  301.       for (incl_sym = sym + STABSIZE;
  302.            incl_sym < symend;
  303.            incl_sym += STABSIZE)
  304.         {
  305.           int incl_type;
  306.  
  307.           incl_type = incl_sym[TYPEOFF];
  308.           if (incl_type == 0)
  309.         break;
  310.           else if (incl_type == N_EINCL)
  311.         {
  312.           if (nest == 0)
  313.             break;
  314.           --nest;
  315.         }
  316.           else if (incl_type == N_BINCL)
  317.         ++nest;
  318.           else if (nest == 0)
  319.         {
  320.           const char *str;
  321.  
  322.           str = ((char *) stabstrbuf
  323.              + stroff
  324.              + bfd_get_32 (abfd, incl_sym + STRDXOFF));
  325.           for (; *str != '\0'; str++)
  326.             {
  327.               val += *str;
  328.               if (*str == '(')
  329.             {
  330.               /* Skip the file number.  */
  331.               ++str;
  332.               while (isdigit ((unsigned char) *str))
  333.                 ++str;
  334.               --str;
  335.             }
  336.             }
  337.         }
  338.         }
  339.  
  340.       /* If we have already included a header file with the same
  341.          value, then replaced this one with an N_EXCL symbol.  */
  342.       incl_entry = stab_link_includes_lookup (&sinfo->includes, string,
  343.                           true, true);
  344.       if (incl_entry == NULL)
  345.         goto error_return;
  346.  
  347.       for (t = incl_entry->totals; t != NULL; t = t->next)
  348.         if (t->total == val)
  349.           break;
  350.  
  351.       /* Record this symbol, so that we can set the value
  352.              correctly.  */
  353.       ne = (struct stab_excl_list *) bfd_alloc (abfd, sizeof *ne);
  354.       ne->offset = sym - stabbuf;
  355.       ne->val = val;
  356.       ne->type = N_BINCL;
  357.       ne->next = secinfo->excls;
  358.       secinfo->excls = ne;
  359.  
  360.       if (t == NULL)
  361.         {
  362.           /* This is the first time we have seen this header file
  363.          with this set of stabs strings.  */
  364.           t = ((struct stab_link_includes_totals *)
  365.            bfd_hash_allocate (&sinfo->includes.root, sizeof *t));
  366.           if (t == NULL)
  367.         goto error_return;
  368.           t->total = val;
  369.           t->next = incl_entry->totals;
  370.           incl_entry->totals = t;
  371.         }
  372.       else
  373.         {
  374.           bfd_size_type *incl_pstridx;
  375.  
  376.           /* We have seen this header file before.  Tell the final
  377.          pass to change the type to N_EXCL.  */
  378.           ne->type = N_EXCL;
  379.  
  380.           /* Mark the skipped symbols.  */
  381.  
  382.           nest = 0;
  383.           for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
  384.            incl_sym < symend;
  385.            incl_sym += STABSIZE, ++incl_pstridx)
  386.         {
  387.           int incl_type;
  388.  
  389.           incl_type = incl_sym[TYPEOFF];
  390.  
  391.           if (incl_type == N_EINCL)
  392.             {
  393.               if (nest == 0)
  394.             {
  395.               *incl_pstridx = (bfd_size_type) -1;
  396.               ++skip;
  397.               break;
  398.             }
  399.               --nest;
  400.             }
  401.           else if (incl_type == N_BINCL)
  402.             ++nest;
  403.           else if (nest == 0)
  404.             {
  405.               *incl_pstridx = (bfd_size_type) -1;
  406.               ++skip;
  407.             }
  408.         }
  409.         }
  410.     }
  411.     }
  412.  
  413.   free (stabbuf);
  414.   free (stabstrbuf);
  415.  
  416.   /* We need to set the section sizes such that the linker will
  417.      compute the output section sizes correctly.  We set the .stab
  418.      size to not include the entries we don't want.  We set
  419.      SEC_EXCLUDE for the .stabstr section, so that it will be dropped
  420.      from the link.  We record the size of the strtab in the first
  421.      .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
  422.      for that section.  */
  423.   stabsec->_cooked_size = (count - skip) * STABSIZE;
  424.   if (stabsec->_cooked_size == 0)
  425.     stabsec->flags |= SEC_EXCLUDE;
  426.   stabstrsec->flags |= SEC_EXCLUDE;
  427.   sinfo->stabstr->_cooked_size = _bfd_stringtab_size (sinfo->strings);
  428.  
  429.   return true;
  430.  
  431.  error_return:
  432.   if (stabbuf != NULL)
  433.     free (stabbuf);
  434.   if (stabstrbuf != NULL)
  435.     free (stabstrbuf);
  436.   return false;
  437. }
  438.  
  439. /* Write out the stab section.  This is called with the relocated
  440.    contents.  */
  441.  
  442. boolean
  443. _bfd_write_section_stabs (output_bfd, stabsec, psecinfo, contents)
  444.      bfd *output_bfd;
  445.      asection *stabsec;
  446.      PTR *psecinfo;
  447.      bfd_byte *contents;
  448. {
  449.   struct stab_section_info *secinfo;
  450.   struct stab_excl_list *e;
  451.   bfd_byte *sym, *tosym, *symend;
  452.   bfd_size_type *pstridx;
  453.  
  454.   secinfo = (struct stab_section_info *) *psecinfo;
  455.  
  456.   if (secinfo == NULL)
  457.     return bfd_set_section_contents (output_bfd, stabsec->output_section,
  458.                      contents, stabsec->output_offset,
  459.                      stabsec->_raw_size);
  460.  
  461.   /* Handle each N_BINCL entry.  */
  462.   for (e = secinfo->excls; e != NULL; e = e->next)
  463.     {
  464.       bfd_byte *excl_sym;
  465.  
  466.       BFD_ASSERT (e->offset < stabsec->_raw_size);
  467.       excl_sym = contents + e->offset;
  468.       bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
  469.       excl_sym[TYPEOFF] = e->type;
  470.     }
  471.  
  472.   /* Copy over all the stabs symbols, omitting the ones we don't want,
  473.      and correcting the string indices for those we do want.  */
  474.   tosym = contents;
  475.   symend = contents + stabsec->_raw_size;
  476.   for (sym = contents, pstridx = secinfo->stridxs;
  477.        sym < symend;
  478.        sym += STABSIZE, ++pstridx)
  479.     {
  480.       if (*pstridx != (bfd_size_type) -1)
  481.     {
  482.       if (tosym != sym)
  483.         memcpy (tosym, sym, STABSIZE);
  484.       bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
  485.       tosym += STABSIZE;
  486.     }
  487.     }
  488.  
  489.   BFD_ASSERT (tosym - contents == stabsec->_cooked_size);
  490.  
  491.   return bfd_set_section_contents (output_bfd, stabsec->output_section,
  492.                    contents, stabsec->output_offset,
  493.                    stabsec->_cooked_size);
  494. }
  495.  
  496. /* Write out the .stabstr section.  */
  497.  
  498. boolean
  499. _bfd_write_stab_strings (output_bfd, psinfo)
  500.      bfd *output_bfd;
  501.      PTR *psinfo;
  502. {
  503.   struct stab_info *sinfo;
  504.  
  505.   sinfo = (struct stab_info *) *psinfo;
  506.  
  507.   if (sinfo == NULL)
  508.     return true;
  509.  
  510.   BFD_ASSERT ((sinfo->stabstr->output_offset
  511.            + _bfd_stringtab_size (sinfo->strings))
  512.           <= sinfo->stabstr->output_section->_raw_size);
  513.  
  514.   if (bfd_seek (output_bfd,
  515.         (sinfo->stabstr->output_section->filepos
  516.          + sinfo->stabstr->output_offset),
  517.         SEEK_SET) != 0)
  518.     return false;
  519.  
  520.   if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
  521.     return false;
  522.  
  523.   /* We no longer need the stabs information.  */
  524.   _bfd_stringtab_free (sinfo->strings);
  525.   bfd_hash_table_free (&sinfo->includes.root);
  526.  
  527.   return true;
  528. }
  529.